package cn.zhiquxing.test;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class Producer implements Runnable{
    //生产的间隔时间
    public static final int product_gap = 200;
    //总次数
    public static final AtomicInteger turn = new AtomicInteger(0);
    // 生产者编号
    static final AtomicInteger producer_no = new AtomicInteger(1);

    public Producer() {
    }
    
    private String name;
    
    private Callable action;
    
    int gap = product_gap;

    public Producer(Callable action, int gap) {
        this.name = "生产者-" + producer_no.getAndIncrement();
        this.action = action;
        this.gap = gap;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Object call = action.call();
                if (call != null) {
                    System.out.println("第" + turn.get() + "轮生产：" + call);
                }
                TimeUnit.MILLISECONDS.sleep(gap);
                turn.incrementAndGet();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
